package com.jiyi.activity;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.v4.content.FileProvider;

import com.jiyi.inteface.PickPopInterface;
import com.jiyi.view.PickPopView;
import com.swift.base.ActivityBase;
import com.swift.maintenance.base.AppConfig;
import com.swift.maintenance.utils.PermissionUtil;
import com.swift.maintenance.utils.ToastUtil;
import com.swift.utils.CameraUtils;

import java.io.File;
import java.util.Date;

/**
 * Created by Administrator on 2018/5/3.
 * 单张图片上传的基类
 */

public class SinglePicActivity extends ActivityBase implements PickPopInterface {

    protected PickPopView mPickPopView;
    // 图片路径
    protected Uri mCameraUri;
    // 拍摄事件
    public final int CAMARE_CODE = 1;
    // 从相册选择事件
    public final int CHOOSE_PICTURE = 2;
    // 图片路径
    protected String filePath;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initSinglePic();
    }

    private void initSinglePic() {
        mPickPopView = new PickPopView(this, this);
    }

    @Override
    public void OnShootListener() {
        if(PermissionUtil.checkPermission(this,PermissionUtil.CARAME))
        {
            useCamere();
        }
    }

    @Override
    public void OnAlbumListener() {
        Intent openAlbumIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(openAlbumIntent, CHOOSE_PICTURE);
        mPickPopView.hidePopWindow();
    }

    @Override
    public void OnCancleListener() {

    }

    @Override
    public void OnShowListener() {

    }

    @Override
    public void OnHideListener() {

    }

    private void useCamere()
    {
        File fileDir = new File(AppConfig.DIR_IMAGE);
        if (!fileDir.exists())
        {
            fileDir.mkdirs();
        }

        filePath = AppConfig.DIR_IMAGE+"temp"+new Date().getTime()+".jpg";
        File file = new File(filePath);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
        {
            mCameraUri = FileProvider.getUriForFile(this,"com.swift.maintenance.updateprovider",file);
        }
        else
        {
            mCameraUri = CameraUtils.getOutputMediaFileUri(CameraUtils.MEDIA_TYPE_IMAGE);
            filePath=mCameraUri.getPath();
        }
        Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");



        intent.putExtra(MediaStore.EXTRA_OUTPUT, mCameraUri);
        startActivityForResult(intent, CAMARE_CODE);
        mPickPopView.hidePopWindow();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == PermissionUtil.CARAME)
        {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED)
            {
                useCamere();
            }
            else
            {
                ToastUtil.getInstances().showMsg("无法访问相机");
            }
        }
    }
}
